﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Rhino.Mocks;
using StrategicDataSystems.Cloud.Monitoring.Configuration;
using StrategicDataSystems.Cloud.Monitoring.HostedServices;
using StrategicDataSystems.Cloud.Monitoring.UI.Services;
using StrategicDataSystems.Cloud.Monitoring.UI.ViewModels;
using StrategicDataSystems.Cloud.Monitoring.UI;
using System.Threading;


namespace StrategicDataSystems.Cloud.Monitoring.Tests.Given_A_SysTrayViewModel
{
    [TestFixture()]
    public class When_Refresh_Command_Is_Fired
    {

        [Test()]
        public void Should_Call_Gateway_To_Check_For_Deployments()
        {
            var mockHostedServiceGateway = MockRepository.GenerateMock<IHostedServiceManagement>();
            var stubNetPresenceService = MockRepository.GenerateStub<INetPresenceService>();
            var stubNotificationService = MockRepository.GenerateStub<INotificationService>();
            var stubTimerService = MockRepository.GenerateStub<ITimerService>();

            var fakeHostedServices = new List<HostedService>();

            Settings settings = new Settings();

            mockHostedServiceGateway.Stub(
                x => x.GetHostedServicesForSubscription(settings.SubscriptionId, settings.CertificateThumbprint))
                .Return(fakeHostedServices);
            stubNetPresenceService.Stub(x => x.InternetConnectionIsAvailable()).Return(true);

            SysTrayViewModel viewModel = new SysTrayViewModel(mockHostedServiceGateway, settings, stubNotificationService, stubTimerService, stubNetPresenceService);

            ((RelayCommand)viewModel.RefreshCommand).Execute(null);

            //Wait for background thread to complete
            Thread.Sleep(10);

            mockHostedServiceGateway.AssertWasCalled(x => x.GetHostedServicesForSubscription(settings.SubscriptionId, settings.CertificateThumbprint), options => options.Repeat.Twice());
        }

        //TODO: There seems to be an issue when you exit greybox that it isn't completely going away..

        [Test()]
        public void Should_Pause_and_Resumed_Timer_During_A_Refresh()
        {
            var stubHostedServiceGateway = MockRepository.GenerateStub<IHostedServiceManagement>();
            var stubNetPresenceService = MockRepository.GenerateStub<INetPresenceService>();
            var stubNotificationService = MockRepository.GenerateStub<INotificationService>();
            var mockTimerService = MockRepository.GenerateMock<ITimerService>();

            var fakeHostedServices = new List<HostedService>() ;

            Settings settings = new Settings();

            stubHostedServiceGateway.Stub(
                x => x.GetHostedServicesForSubscription(settings.SubscriptionId, settings.CertificateThumbprint))
                .Return(fakeHostedServices);
            stubNetPresenceService.Stub(x => x.InternetConnectionIsAvailable()).Return(true);

            SysTrayViewModel viewModel = new SysTrayViewModel(stubHostedServiceGateway, settings, stubNotificationService, mockTimerService, stubNetPresenceService);

            ((RelayCommand)viewModel.RefreshCommand).Execute(null);

            //Wait for background thread to complete
            Thread.Sleep(10);

            mockTimerService.AssertWasCalled(x => x.Pause(), options => options.Repeat.Once());
            mockTimerService.AssertWasCalled(x => x.Resume(), options => options.Repeat.Once());
        }


       
    }
}
